Component({
  options: {
    multipleSlots: true
  },
  properties: {
    label: { // 表单label
      type: String,
      value: ''
    },
    placeholder: { // 占位提示
      type: String,
      value: ''
    },
    value: { // 默认日期值
      type: String,
      value: ''
    },
    type: { // 类型 datetime/date/time
      type: String,
      value: 'datetime'
    },
    color: { // 确认按钮颜色
      type: String,
      value: '#3565ee'
    },
    startYear: { // 开始年份 type为datetime/date生效
      type: [String,Number],
      value: 1970
    },
    endYear: { // 结束年份 type为datetime/date生效
      type: [String,Number],
      value: new Date().getFullYear()
    },
    step: { // 分钟步长 type为datetime/time生效
      type: [String,Number],
      value: 1
    },
    maskClose: { // 是否开启点击遮罩关闭弹窗
      type: [Boolean, String],
      value: true
    },
    separator: { // 输出的日期分隔符 type为datetime/date生效
      type: String,
      value: '-'
    },
    showArrow: { // 是否显示箭头
      type: [Boolean, String],
      value: true
    },
    showBorder: { // 是否显示线条
      type: [Boolean, String],
      value: false
    },
    disabled: { // 是否禁用
      type: [Boolean, String],
      value: false
    }
  },
  data: {
    pickerViewStyle:`height:44px;`,
    showDatetime:false,
    years:[],
    months:[],
    days:[],
    hours:[],
    minutes:[],
    year:'',
    month:'',
    day:'',
    hour:'',
    minute:'',
    values:[0,0,0,0,0],
    valueArr:[], //picker变化后的值
    valueShow:'' //默认需要显示的值或选中的显示值
  },
  lifetimes: {
    ready: function () {
      this.setData({
        valueShow:this.data.value
      })
      this.judgeInit();
    }
  },
  methods: {
    //数据初始化判断
    judgeInit(){
      if(this.data.type == 'datetime'){
        this.initDatetimeData();
      }else if(this.data.type == 'date'){
        this.initDateData();
      }else if(this.data.type == 'time'){
        this.initTimeData();
      }
    },
    //点击、滑动遮罩，判断是否关闭时间日期对话框
    handleMask(){
      if(this.data.maskClose){
        this.setData({
          showDatetime:false
        })
        this.judgeInit();
      }
    },
    //显示时间日期对话框
    handleShowPicker(){
      this.setData({
        showDatetime:true
      })
    },
    //获取月份天数
    getDays(year, month){
      let days = new Date(year, month, 0);
      return days.getDate();
    },
    //日期时间的初始化
    formatData(value) { 
      return value < 10 ? '0' + value : value + '';
    },
    //type为datetime数据初始化
    initDatetimeData(){
      let years = [],months = [],days = [],hours = [],minutes = [], year = 1970,month = 1,day = 1,hour = 0,minute = 0,totalDays = 0,values=[0,0,0,0,0];
      let str = this.data.valueShow;
      if(str){
        // 有默认值显示默认值
        let arr = str.split(/\/|-|[ ]|:/);
        year = parseInt(arr[0]);
        month = parseInt(arr[1]);
        day = parseInt(arr[2]);
        hour = parseInt(arr[3]);
        minute = parseInt(arr[4]);
        totalDays = this.getDays(year,month); //当前月份总天数
      }else{
        // 无默认值显示当前日期时间
        let date = new Date();
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        hour = date.getHours();
        minute = date.getMinutes();
        totalDays = this.getDays(year,month); //当前月份总天数
      }
			for (let i = parseInt(this.data.startYear); i <= this.data.endYear; i++) {
        values[0] = parseInt(year - this.data.startYear);
				years.push(this.formatData(i));
			}
			for (let i = 1; i <= 12; i++) {
        if(month == i) {values[1] = parseInt(i - 1);}
				months.push(this.formatData(i));
			}
			for (let i = 1; i <= totalDays; i++) {
        if(day == i) {values[2] = parseInt(i - 1);}
				days.push(this.formatData(i));
			}
			for (let i = 0; i < 24; i++) {
        if(hour == i) {values[3] = parseInt(i);}
				hours.push(this.formatData(i));
      }
      let step  = (this.data.step > 1)? parseInt(this.data.step) : 1;
			for (let i = 0; i < 60; i++) {
        // if(minute == i) {values[4] = parseInt(i);}
        // minutes.push(this.formatData(i));
        if(minute == i && minute <= 60 - step) {
          values[4] = Math.ceil(minute/step);
        }else if(minute == i && minute > 60 - step) {
          values[4] = Math.floor(minute/step);
        }
        if(i%step == 0){
          minutes.push(this.formatData(i));
        }
      }
      minute = minutes[values[4]]; //分钟默认值要匹配到跟步长相关的分钟值
      this.data.valueArr = [this.formatData(year),this.formatData(month),this.formatData(day),this.formatData(hour),minute];
      this.setData({
        years,months,days,hours,minutes,year,month,day,hour,minute
      })
      //这里是value的坑，需要单独设值
      this.setData({
        values
      })
    },
    //type为date数据初始化
    initDateData(){
      let years = [],months = [],days = [], year = 1970,month = 1,day = 1,totalDays = 0,values=[0,0,0];
      let str = this.data.valueShow;
      if(str){
        // 有默认值显示默认值
        let arr = str.split(/\/|-|[ ]|:/);
        year = parseInt(arr[0]);
        month = parseInt(arr[1]);
        day = parseInt(arr[2]);
        totalDays = this.getDays(year,month); //当前月份总天数
      }else{
        // 无默认值显示当前日期时间
        let date = new Date();
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        totalDays = this.getDays(year,month); //当前月份总天数
      }
			for (let i = parseInt(this.data.startYear); i <= this.data.endYear; i++) {
        values[0] = parseInt(year - this.data.startYear);
				years.push(this.formatData(i));
			}
			for (let i = 1; i <= 12; i++) {
        if(month == i) {values[1] = parseInt(i - 1);}
				months.push(this.formatData(i));
			}
			for (let i = 1; i <= totalDays; i++) {
        if(day == i) {values[2] = parseInt(i - 1);}
				days.push(this.formatData(i));
			}
      this.data.valueArr = [this.formatData(year),this.formatData(month),this.formatData(day)];
      this.setData({
        years,months,days,year,month,day
      })
      //这里是value的坑，需要单独设值
      this.setData({
        values
      })
    },
    //type为time时数据初始化
    initTimeData(){
      let hours = [],minutes = [], hour = 0,minute = 0,values=[0,0];
      let str = this.data.valueShow;
      if(str){
        // 有默认值显示默认值
        let arr = str.split(':');
        hour = parseInt(arr[0]);
        minute = parseInt(arr[1]);
      }else{
        // 无默认值显示当前日期时间
        let date = new Date();
        hour = date.getHours();
        minute = date.getMinutes();
      }
      for (let i = 0; i < 24; i++) {
        if(hour == i) {values[0] = parseInt(i);}
				hours.push(this.formatData(i));
      }
      let step  = (this.data.step > 1)? parseInt(this.data.step) : 1;
			for (let i = 0; i < 60; i++) {
        if(minute == i && minute <= 60 - step) {
          values[4] = Math.ceil(minute/step);
        }else if(minute == i && minute > 60 - step) {
          values[4] = Math.floor(minute/step);
        }
        if(i%step == 0){
          minutes.push(this.formatData(i));
        }
      }
      
      minute = minutes[values[4]]; //分钟默认值要匹配到跟步长相关的分钟值
      this.data.valueArr = [this.formatData(hour),this.formatData(minute)];
      this.setData({
        hours,minutes,hour,minute
      })
      //这里是value的坑，需要单独设值
      this.setData({
        values
      })
    },
    //picker变化时执行
    handleChange(e){
      let values = e.detail.value;
      let val = [],days=[],year=0,month=0,totalDays=0;
      if(this.data.type == 'datetime' || this.data.type == 'date'){
        //改变年月要重新设置天数
        year = this.data.years[values[0]];
        month = this.data.months[values[1]];
        totalDays = this.getDays(year,month);
        for (let i = 1; i <= totalDays; i++) {
          days.push(this.formatData(i));
        }
        this.setData({days,values});
      }else if(this.data.type == 'time'){
        this.setData({values});
      }
      //构造变化的输出值
      if(this.data.type == 'datetime'){
        val[0] = year;
        val[1] = month;
        val[2] = this.data.days[values[2]];
        val[3] = this.data.hours[values[3]];
        val[4] = this.data.minutes[values[4]];
      }else if(this.data.type == 'date'){
        val[0] = year;
        val[1] = month;
        val[2] = this.data.days[values[2]];
      }else if(this.data.type == 'time'){
        val[0] = this.data.hours[values[0]];
        val[1] = this.data.minutes[values[1]];
      }
      this.data.valueArr = val;
      this.triggerEvent('change', val)
    },
    //取消选择
    pickerCancel(){
      this.setData({
        showDatetime:false
      })
      this.judgeInit();
    },
    //确认选择
    pickerConfirm(){
      let arr = this.data.valueArr;
      let sign = this.data.separator;//分隔符
      let obj = {
        value:'',
        values:arr
      }
      //构造确认输出值
      if(this.data.type == 'datetime'){
        obj.value = arr[0] + sign + arr[1] + sign + arr[2] + ' ' + arr[3] + ':' + arr[4];
      }else if(this.data.type == 'date'){
        obj.value = arr[0] + sign + arr[1] + sign + arr[2];
      }else if(this.data.type == 'time'){
        obj.value = arr[0] + ':' + arr[1];
      }
      this.setData({
        valueShow:obj.value,
        showDatetime:false
      })
      this.triggerEvent('confirm', obj)
    }
  }
})
